function indexJs() {
    // 菜单盒子
    var menu = document.querySelector('.menu')
        // 推荐商品盒子
    var moneyCtrl = document.getElementById('moneyCtrl')
        // 返回顶部按钮
    var top0 = document.getElementById('top0')
        // console.log(top0);
        // console.log(menu);
        // console.log(moneyCtrl);
        // 动态渲染商品推荐
    function render(data) {
        let html = ''
        data.forEach(item => {
            html += `<a href="moneyproduct.html?productId=${item.productId}">
        <div class="imgBox">
            ${item.productImg2}
        </div>
        <div class="textBox">
            <span>
                <span> ${item.productName}</span>
                <span>${item.productPinkage}</span>
            </span>
            <div>
                <span>${item.productFrom}</span>
                <span>${item.productTime}</span>
                <span>${item.productComCount}</span>
            </div>
        </div>
    </a>`
        });
        return html;
    }
    // 折扣商品数据申请
    ajax({
            url: "http://chst.vip:1234/api/getmoneyctrl?key=all",
            success(res) {
                // console.log(res.result);
                let data = res.result;
                // console.log(data);
                let html = render(data);
                moneyCtrl.innerHTML = html;
            }
        })
        // 菜单数据申请
    ajax({
            url: "http://chst.vip:1234/api/getindexmenu?key=all",
            success(res) {
                let data = res.result;
                var firstData = data.slice(0, 8)
                renderList(firstData);
                let more = menu.children
                console.log(more);
                let flag = true
                more[7].onclick = function() {
                    if (flag) {
                        renderList(data);
                        flag = !flag
                    } else {
                        renderList(firstData);
                        flag = !flag
                    }


                }

                function renderList(arr) {
                    let html = '';
                    arr.forEach(item => {
                        let srcStr = item.img
                        let src = srcStr.substring(10, srcStr.length)
                        html += `
                    <a>
                                <div>
                                  <img src="../${src}
                                </div>
                                <em>${item.name}</em>
                            </a>`
                            // console.log(src);
                    });
                    menu.innerHTML = html;
                    for (let i = 0; i < menu.children.length; i++) {
                        // let data = res.result;
                        // var firstData = data.slice(0, 8)
                        menu.children[i].onclick = function() {
                            if (i === 7) {
                                if (flag) {
                                    console.log(123);
                                    renderList(data);
                                    flag = !flag
                                } else {
                                    renderList(firstData);
                                    console.log(456);
                                    flag = !flag
                                }
                            } else {
                                location.href = data[i].titlehref
                                console.log(data[i].titlehref);
                            }
                        }
                    }
                }
            }
        })
        // 动态渲染菜单函数
    function renderList2(data) {
        let html = '';
        data.forEach(item => {
            let srcStr = item.img
            let src = srcStr.substring(10, srcStr.length)
            html += `
            <a>
                        <div>
                          <img src="../${src}
                        </div>
                        <em>${item.name}</em>
                    </a>`
                // console.log(src);
        });
    }
    let main = document.querySelector('#main')
        // 返回顶部按钮功能实现
    main.onscroll = function() {
        top0.onclick = function() {
            let  timer  =  setInterval(()  =>  {   
                main.scrollTop  -= 20   
                if  (main.scrollTop  <=  0)  {                
                    clearInterval(timer)            
                }
            }, 10)
        }
    }
}
indexJs()